package abq2.jic.jic.plugin.workflow;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.workflow.api.AgentExecution;
import kd.bos.workflow.engine.extitf.IWorkflowPlugin;
import kd.sdk.plugin.Plugin;

/**
 * 工作流插件
 */
public class BookBorrowWork implements Plugin, IWorkflowPlugin {
    @Override
    public void notify(AgentExecution execution) {
        IWorkflowPlugin.super.notify(execution);
        String entityNumber = execution.getEntityNumber();

        DynamicObject option = BusinessDataServiceHelper.loadSingle("abq2_books_borrow",
                        "billno," + //单据编号
                        "id," + //id值
                        "entryentity," + //单据体
                        "abq2_textfield222," + //借阅状态
                        "abq2_basedatafield," + //书籍信息
                        "creator" //借阅者
                        ,
                new QFilter[]{new QFilter("billno", QCP.equals, entityNumber)});
        option.set("abq2_textfield222","借阅中");
        DynamicObjectCollection books = option.getDynamicObjectCollection("entryentity");
        for (DynamicObject book : books) {
            long id = book.getLong("abq2_basedatafield.id");
            DynamicObject bookInfo = BusinessDataServiceHelper.loadSingle("abq2_books_info",
                    "id,abq2_integerfield,abq2_integerfield1",
                    new QFilter[]{new QFilter("id", QCP.equals, id)});
            bookInfo.set("abq2_integerfield", bookInfo.getInt("abq2_integerfield") - 1);
            bookInfo.set("abq2_integerfield1", bookInfo.getInt("abq2_integerfield1") + 1);
            SaveServiceHelper.update(bookInfo);
        }
        SaveServiceHelper.update(option);
    }
}